package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.system.api.domain.CountryTwoCode;
import com.ruoyi.system.domain.bo.CountryTwoCodeBo;
import com.ruoyi.system.domain.vo.CountryTwoCodeVo;
import com.ruoyi.system.mapper.CountryTwoCodeMapper;
import com.ruoyi.system.service.ICountryTwoCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * 国家二字码 服务层实现
 *
 * @author ruoyi
 */
@Service
public class CountryTwoCodeServiceImpl implements ICountryTwoCodeService
{
    @Autowired
    private CountryTwoCodeMapper baseMapper;


    /**
     * 查询国家二字码
     */
    @Override
    public CountryTwoCodeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询国家二字码列表
     */
    @Override
    public TableDataInfo<CountryTwoCodeVo> queryPageList(CountryTwoCodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CountryTwoCode> lqw = buildQueryWrapper(bo);
        Page<CountryTwoCodeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询国家二字码列表
     */
    @Override
    public List<CountryTwoCodeVo> queryList(CountryTwoCodeBo bo) {
        LambdaQueryWrapper<CountryTwoCode> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CountryTwoCode> buildQueryWrapper(CountryTwoCodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CountryTwoCode> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCountryCn()), CountryTwoCode::getCountryCn, bo.getCountryCn());
        lqw.eq(StringUtils.isNotBlank(bo.getCountryEn()), CountryTwoCode::getCountryEn, bo.getCountryEn());
        lqw.eq(StringUtils.isNotBlank(bo.getIsoCode()), CountryTwoCode::getIsoCode, bo.getIsoCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), CountryTwoCode::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增国家二字码
     */
    @Override
    public Boolean insertByBo(CountryTwoCodeBo bo) {
        CountryTwoCode add = BeanUtil.toBean(bo, CountryTwoCode.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改国家二字码
     */
    @Override
    public Boolean updateByBo(CountryTwoCodeBo bo) {
        CountryTwoCode update = BeanUtil.toBean(bo, CountryTwoCode.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CountryTwoCode entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除国家二字码
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据国家中文名获取国家二字码
     * @param countryCn
     * @return
     */
    @Override
    public CountryTwoCode getByCountryCn(String countryCn){
        return baseMapper.selectOne(new LambdaQueryWrapper<CountryTwoCode>().eq(CountryTwoCode::getCountryCn, countryCn));
    }

    @Override
    public CountryTwoCode getCountryByCode(String code) {
        
        return baseMapper.selectOne(new LambdaQueryWrapper<CountryTwoCode>().eq(CountryTwoCode::getIsoCode,code));
    }
}
